<?php
	  
require_once("DatabaseConnect.php");
require_once("PeclStrats.php");//Holds all of the pecl trader technical calcultion functions

//stoping the page from timeing out
set_time_limit(130);
//Database connection
$con = Remoteconnect();
if ($con == null) {
    $con = Localconnect();
}

function CheckPortfolio($con){
	echo "<table width='200' border='1'>";

    $Email = $_SESSION['email'];
	
    //Initial Setup of variables
	//Iterate throuugh the users portfolio of shares.
	$result = mysqli_query($con,"SELECT * FROM  `userportfolio` WHERE `Email`='$Email'");
	while($row = mysqli_fetch_array($result)) {
		
			
	
    $Symbol = $row['Symbol'];	

	
	//get start date
	$Date= mysqli_query($con, "SELECT * FROM `shares` WHERE  `Symbol` =  '$Symbol' Order By `Date` DESC Limit 1
");


    while ($row = mysqli_fetch_array($Date)) {

	$StartDate=$row['Date'];
	$EndDate=$row['Date'];	
	}
	//Iterate through the users strategies
		$res = mysqli_query($con,"SELECT * FROM  `strategies` WHERE `Email`='$Email'");
	while($row = mysqli_fetch_array($res)) {
	
	$Name=$row['Name'];



	
    //First buy signal
    $Buy1_1 = $row['Buy1_1'];
    $Buy1_2 = $row['Buy1_2'];
    $Buy1_3 = $row['Buy1_3'];
    $BuyInput1_1 = $row['BuyInput1_1'];
    $BuyInput1_2 = $row['BuyInput1_2'];
    $BuyInput1_3 = $row['BuyInput1_3'];

    //First sell signal
    $Sell1_1 = $row['Sell1_1'];
    $Sell1_2 = $row['Sell1_2'];
    $Sell1_3 = $row['Sell1_3'];
    $SellInput1_1 = $row['SellInput1_1'];
    $SellInput1_2 = $row['SellInput1_2'];
    $SellInput1_3 = $row['SellInput1_3'];

	

	
    $indexer = 1;//used to iterate through the technical analysis arrays
	
	
	//Calculate the technical anaylysis arrays using the pecl trader php extension
	if($Buy1_1=="SMA")
   		$SMAArrayBuy1_1 = SMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
	if($Buy1_3=="SMA")
    	$SMAArrayBuy1_3 = SMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="SMA")
		$SMAArraySell_1 = SMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="SMA")
		$SMAArraySell1_3 = SMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
  	
	if($Buy1_1=="EMA")
		$EMAArrayBuy1_1 = EMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="EMA")
		$EMAArrayBuy1_3 = SMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="EMA")
		$EMAArraySell_1 = EMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="EMA")
		$EMAArraySell1_3 = EMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
   	
	if($Buy1_1=="RSI")
		$RSIArrayBuy1_1 = RSI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
		
    if($Buy1_3=="RSI")
		$RSIArrayBuy1_3 = RSI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="RSI")
		$RSIArraySell_1 = RSI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="RSI")
		$RSIArraySell1_3 = RSI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
			
	if($Buy1_1=="SAR")
		$SARArrayBuy1_1 = SAR($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="SAR")
		$SARArrayBuy1_3 = SAR($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="SAR")
		$SARArraySell_1 = SAR($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="SAR")
   		$SARArraySell1_3 = SAR($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);

	if($Buy1_1=="WMA")
		$WMAArrayBuy1_1 = WMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="WMA")
		$WMAArrayBuy1_3 = WMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="WMA")
		$WMAArraySell_1 = WMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="WMA")
   		$WMAArraySell1_3 = WMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		
		
	if($Buy1_1=="BollingerUpper")
		$BollingerUpperArrayBuy1_1 = Bollinger($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);

    if($Buy1_3=="BollingerUpper")
		$BollingerUpperArrayBuy1_3 = Bollinger($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
		
    if($Sell1_1=="BollingerUpper")
		$BollingerUpperArraySell_1 = Bollinger($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
		
    if($Sell1_3=="BollingerUpper")
   		$BollingerUpperArraySell1_3 = Bollinger($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		
	if($Buy1_1=="BollingerLower")
		$BollingerLowerArrayBuy1_1 = Bollinger($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="BollingerLower")
		$BollingerLowerArrayBuy1_3 = Bollinger($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="BollingerLower")
		$BollingerLowerArraySell_1 = Bollinger($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="BollingerLower")
   		$BollingerLowerArraySell1_3 = Bollinger($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		

	if($Buy1_1=="MacD")
		$MacDArrayBuy1_1 = MacD($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MacD")
		$MacDArrayBuy1_3 = MacD($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MacD")
		$MacDArraySell_1 = MacD($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MacD")
   		$MacDArraySell1_3 = MacD($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		

	if($Buy1_1=="MacDSignal")
		$MacDSignalArrayBuy1_1 = MacD($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MacDSignal")
		$MacDSignalArrayBuy1_3 = MacD($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MacDSignal")
		$MacDSignalArraySell_1 = MacD($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MacDSignal")
   		$MacDSignalArraySell1_3 = MacD($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="ROC")
		$ROCArrayBuy1_1 = ROC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="ROC")
		$ROCArrayBuy1_3 = ROC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="ROC")
		$ROCArraySell_1 = ROC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="ROC")
   		$ROCArraySell1_3 = ROC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
		
	if($Buy1_1=="OBV")
		$OBVArrayBuy1_1 = OBV($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="OBV")
		$OBVArrayBuy1_3 = OBV($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="OBV")
		$OBVArraySell_1 = OBV($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="OBV")
   		$OBVArraySell1_3 = OBV($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
		
	if($Buy1_1=="PVT")
		$PVTArrayBuy1_1 = PVT($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="PVT")
		$PVTArrayBuy1_3 = PVT($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="PVT")
		$PVTArraySell_1 = PVT($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="PVT")
   		$PVTArraySell1_3 = PVT($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
		
	if($Buy1_1=="WilliamR")
		$WilliamRArrayBuy1_1 = WilliamR($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="WilliamR")
		$WilliamRArrayBuy1_3 = WilliamR($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="WilliamR")
		$WilliamRArraySell_1 = WilliamR($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="WilliamR")
   		$WilliamRArraySell1_3 = WilliamR($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="MTM")
		$MTMArrayBuy1_1 = MTM($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MTM")
		$MTMArrayBuy1_3 = MTM($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MTM")
		$MTMArraySell_1 = MTM($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MTM")
   		$MTMArraySell1_3 = MTM($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
		
	if($Buy1_1=="fastSTCk")
		$fastSTCkArrayBuy1_1 = FastSTC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="fastSTCk")
		$fastSTCkArrayBuy1_3 = FastSTC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="fastSTCk")
		$fastSTCkArraySell_1 = FastSTC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="fastSTCk")
   		$fastSTCkArraySell1_3 = FastSTC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		

	if($Buy1_1=="fastSTCd")
		$fastSTCdArrayBuy1_1 = FastSTC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="fastSTCd")
		$fastSTCdArrayBuy1_3 = FastSTC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="fastSTCd")
		$fastSTCdArraySell_1 = FastSTC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="fastSTCd")
   		$fastSTCdArraySell1_3 = FastSTC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
		
	if($Buy1_1=="DMIplus")
		$DMIplusArrayBuy1_1 = DMI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
		
    if($Buy1_3=="DMIplus")
		$DMIplusArrayBuy1_3 = DMI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="DMIplus")
		$DMIplusArraySell_1 = DMI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="DMIplus")
   		$DMIplusArraySell1_3 = DMI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	
	if($Buy1_1=="DMIminus")
		$DMIminusArrayBuy1_1 = DMI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="DMIminus")
		$DMIminusArrayBuy1_3 = DMI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="DMIminus")
		$DMIminusArraySell_1 = DMI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="DMIminus")
   		$DMIminusArraySell1_3 = DMI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		


    //obtain the Moast recent data entry int he database for each symbol in the portfolio
    $historicalData = mysqli_query($con, "SELECT * FROM `shares` WHERE  `Symbol` =  '$Symbol' Order By `Date` DESC Limit 1
");


    while ($row = mysqli_fetch_array($historicalData)) {

        $currentPriceOfShares = $row['Close'];
        $thedate = $row['Date'];
	
        //check if buy signal is met:
            switch ($Buy1_1) {
                case "SMA":
                    $Buy1 = $SMAArrayBuy1_1[$indexer-1];

                    break;
                case "EMA":
                    $Buy1 = $EMAArrayBuy1_1[$indexer-1];

                    break;
                case "Close":
                    $Buy1 = $row['Close'];
                    break;
                case "Open":
                    $Buy1 = $row['Open'];
                    break;
                case "High":
                    $Buy1 = $row['High'];
                    break;
                case "Low":
                    $Buy1 = $row['Low'];
                    break;
                case "Volume":
                    $Buy1 = $row['Volume'];
                    break;
				case "RSI":
					$Buy1= $RSIArrayBuy1_1[$indexer-1];
					break;
				case "Value":
					$Buy1= $BuyInput1_1;
					break;
				case "SAR":
					$Buy1= $SARArrayBuy1_1[0][$indexer];
					break;
				case "WMA":
					$Buy1= $WMAArrayBuy1_1[$indexer-1];
					break;
				case "BollingerUpper":
					$Buy1= $BollingerUpperArrayBuy1_1[0][$indexer-1];
					break;
				case "BollingerLower":
					$Buy1= $BollingerLowerArrayBuy1_1[2][$indexer-1];
					break;
				case "MacD":
					$Buy1= $MacDArrayBuy1_1[0][$indexer-1];
					break;
				case "MacDSignal":
					$Buy1= $MacDSignalArrayBuy1_1[1][$indexer-1];
					break;
				case "ROC":
					$Buy1= $ROCArrayBuy1_1[$indexer-1];
					break;
				case "OBV":
					$Buy1= $OBVArrayBuy1_1[0][$indexer-1];
					break;
				case "WilliamR":
					$Buy1= $WilliamRArrayBuy1_1[$indexer-1];
					break;
				case "MTM":
					$Buy1= $MTMArrayBuy1_1[$indexer-1];
					break;
				case "fastSTCk":
					$Buy1= $fastSTCkArrayBuy1_1[0][$indexer-1];
					break;
				case "fastSTCd":
					$Buy1= $fastSTCdArrayBuy1_1[1][$indexer-1];
					break;
				case "DMIplus":
					$Buy1= $DMIplusArrayBuy1_1[0][$indexer-1];
					break;
				case "DMIminus":
					$Buy1= $DMIminusArrayBuy1_1[1][$indexer-1];
					break;
				case "PVT":
					$Buy1= $PVTArrayBuy1_1[$indexer-1];
					break;
					
            }


            //check $Buy1_3
            switch ($Buy1_3) {
                case "SMA":
                    $Buy3 = $SMAArrayBuy1_3[$indexer-1];
                    break;
                case "EMA":
                    $Buy3 = $EMAArrayBuy1_3[$indexer-1];
                    break;
                case "Close":
                    $Buy3 = $row['Close'];
                    break;
                case "Open":
                    $Buy3 = $row['Open'];
                    break;
                case "High":
                    $Buy3 = $row['High'];
                    break;
                case "Low":
                    $Buy3 = $row['Low'];
                    break;
                case "Volume":
                    $Buy3 = $row['Volume'];
                    break;
				case "RSI":
					$Buy3= $RSIArrayBuy1_3[$indexer-1];
					break;
				case "Value":
					$Buy3= $BuyInput1_3;
					break;
				case "SAR":
					$Buy3= $SARArrayBuy1_3[0][$indexer];
					break;
				case "WMA":
					$Buy3= $WMAArrayBuy1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$Buy3= $BollingerUpperArrayBuy1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$Buy3= $BollingerLowerArrayBuy1_3[2][$indexer-1];
					break;
				case "MacD":
					$Buy3= $MacDArrayBuy1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$Buy3= $MacDSignalArrayBuy1_3[1][$indexer-1];
					break;
				case "ROC":
					$Buy3= $ROCArrayBuy1_3[$indexer-1];
					break;
				case "OBV":
					$Buy3= $OBVArrayBuy1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$Buy3= $WilliamRArrayBuy1_3[$indexer-1];
					break;
				case "MTM":
					$Buy3= $MTMArrayBuy1_3[$indexer-1];
					break;
				case "fastSTCk":
					$Buy3= $fastSTCkArrayBuy1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$Buy3= $fastSTCdArrayBuy1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$Buy3= $DMIplusArrayBuy1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$Buy3= $DMIminusArrayBuy1_3[1][$indexer-1];
					break;
				case "PVT":
					$Buy3= $PVTArrayBuy1_3[$indexer-1];
					break;
					
					
            }
            //Test buy signal algorithm here:
            switch ($Buy1_2) {
                case "Above":						

                    //Buy1_1>Buy1_3
                    if ($Buy1 > $Buy3) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];
						
						
						echo"<tr>";
						echo"<td>Buy Signal found for $Symbol, using $Name strategy: Buy when: $Buy1_1($BuyInput1_1)=[$Buy1] is above $Buy1_3($BuyInput1_3)=[$Buy3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
						
                    }
                    break;
                case "Below":
                    //Buy1_1<Buy1_3
					if ($Buy1 < $Buy3) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];
						
						echo"<tr>";
						echo"<td>Buy Signal found for $Symbol, using $Name strategy: Buy when: $Buy1_1($BuyInput1_1)=[$Buy1] is below $Buy1_3($BuyInput1_3)=[$Buy3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
					}
                    break;
                case "AtLeast%Above%":

                    $percentage = CalculatePercentage($Buy1, $Buy3);

					
                    if ($percentage > $BuyInput1_2) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];
						echo"<tr>";
						echo"<td>Buy Signal found for $Symbol, using $Name strategy: Buy when: $Buy1_1($BuyInput1_1)=[$Buy1] is at least $BuyInput1_2% above $Buy1_3($BuyInput1_3)=[$Buy3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";

					}
                    break;
                case "AtLeast%Below%":

                    $percentage = CalculatePercentage($Buy1, $Buy3);

                    if ($percentage < -$BuyInput1_2) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];
						echo"<tr>";
						echo"<td>Buy Signal found for $Symbol, using $Name strategy: Buy when: $Buy1_1($BuyInput1_1)=[$Buy1] is atleast $BuyInput1_2% below $Buy1_3($BuyInput1_3)=[$Buy3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
					}
                    break;
            }
       

           //check if any sell signal is met

            switch ($Sell1_1) {
                case "SMA":
                    $Sell1 = $SMAArraySell_1[$indexer-1];
                    break;
                case "EMA":
                    $Sell1 = $EMAArraySell_1 [$indexer-1];
                    break;
                case "Close":
                    $Sell1 = $row['Close'];
                    break;
                case "Open":
                    $Sell1 = $row['Open'];
                    break;
                case "High":
                    $Sell1 = $row['High'];
                    break;
                case "Low":
                    $Sell1 = $row['Low'];
                    break;
                case "Volume":
                    $Sell1 = $row['Volume'];
                    break;
				case "RSI":
					$Sell1 = $RSIArraySell_1[$indexer-1];
					break;
				case "Value":
					$Sell1 = $SellInput1_1;
					break;
				case "SAR":
					$Sell1 = $SARArraySell_1[0][$indexer];
					break;
				case "WMA":
					$Sell1 = $WMAArraySell_1[$indexer-1];
					break;
				case "BollingerUpper":
					$Sell1 = $BollingerUpperArraySell_1[0][$indexer-1];
					break;
				case "BollingerLower":
					$Sell1 = $BollingerLowerArraySell_1[2][$indexer-1];
					break;
				case "MacD":
					$Sell1 = $MacDArraySell_1[0][$indexer-1];
					break;
				case "MacDSignal":
					$Sell1 = $MacDSignalArraySell_1[1][$indexer-1];
					break;
				case "ROC":
					$Sell1 = $ROCArraySell_1[$indexer-1];
					break;
				case "OBV":
					$Sell1 = $OBVArraySell_1[0][$indexer-1];
					break;
				case "WilliamR":
					$Sell1 = $WilliamRArraySell_1[$indexer-1];
					break;
				case "MTM":
					$Sell1 = $MTMArraySell_1[$indexer-1];
					break;
				case "fastSTCk":
					$Sell1 = $fastSTCkArraySell_1[0][$indexer-1];
					break;
				case "fastSTCd":
					$Sell1 = $fastSTCdArraySell_1[1][$indexer-1];
					break;
				case "DMIplus":
					$Sell1 = $DMIplusArraySell_1[0][$indexer-1];
					break;
				case "DMIminus":
					$Sell1 = $DMIminusArraySell_1[1][$indexer-1];
					break;
				case "PVT":
					$Sell1 = $PVTArraySell_1[$indexer-1];
					break;
			}
				
			
			//check $Sell1_3
			switch ($Sell1_3) {
				case "SMA":
					$Sell3 = $SMAArraySell1_3[$indexer-1];
					break;
				case "EMA":
					$Sell3 = $EMAArraySell1_3 [$indexer-1];
					break;
				 case "Close":
                    $Sell3 = $row['Close'];
                    break;
                case "Open":
                    $Sell3 = $row['Open'];
                    break;
                case "High":
                    $Sell3 = $row['High'];
                    break;
                case "Low":
                    $Sell3 = $row['Low'];
                    break;
                case "Volume":
                    $Sell3 = $row['Volume'];
                    break;
				case "RSI":
					$Sell3 = $RSIArraySell1_3[$indexer-1];
					break;
				case "Value":
					$Sell3 = $SellInput1_3;
					break;
				case "SAR":
					$Sell3 = $SARArraySell1_3[0][$indexer];
					break;
				case "WMA":
					$Sell3 = $WMAArraySell1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$Sell3 = $BollingerUpperArraySell1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$Sell3 = $BollingerLowerArraySell1_3[2][$indexer-1];
					break;
				case "MacD":
					$Sell3 = $MacDArraySell1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$Sell3 = $MacDSignalArraySell1_3[1][$indexer-1];
					break;
				case "ROC":
					$Sell3 = $ROCArraySell1_3[$indexer-1];
					break;
				case "OBV":
					$Sell3 = $OBVArraySell1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$Sell3 = $WilliamRArraySell1_3[$indexer-1];
					break;
				case "MTM":
					$Sell3 = $MTMArraySell1_3[$indexer-1];
					break;
				case "fastSTCk":
					$Sell3 = $fastSTCkArraySell1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$Sell3 = $fastSTCdArraySell1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$Sell3 = $DMIplusArraySell1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$Sell3 = $DMIminusArraySell1_3[1][$indexer-1];
					break;
				case "PVT":
					$Sell3 = $PVTArraySell1_3[$indexer-1];
					break;
					
	
			}
			
            //Test sell signal algorithm here:

            switch ($Sell1_2) {
                case "Above":

                    //Sell1_1>Sell1_3
                    if ($Sell1 > $Sell3) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];
						echo"<tr>";
						echo"<td>Sell Signal found for $Symbol, using $Name strategy: Sell when: $Sell1_1($SellInput1_1)=[$Sell1] is above $Sell1_3($SellInput1_3)=[$Sell3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
                    }
                    break;
                case "Below":
                    //Sell1_1<Sell1_3

                    if ($Sell1 < $Sell3) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];

						echo"<tr>";
						echo"<td>Sell Signal found for $Symbol, using $Name strategy: Sell when: $Sell1_1($SellInput1_1)=[$Sell1] is below $Sell1_3($SellInput1_3)=[$Sell3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
                    }
                    break;
                case "AtLeast%Above%":
                    $percentage = CalculatePercentage($Sell1, $Sell3);


                    if ($percentage > $SellInput1_2) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];

						echo"<tr>";
						echo"<td>Sell Signal found for $Symbol, using $Name strategy: Sell when: $Sell1_1($SellInput1_1)=[$Sell1] is at least $SellInput1_2% above $Sell1_3($SellInput1_3)=[$Sell3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
                    }
                    break;
                case "AtLeast%Below%":
                    $percentage = CalculatePercentage($Sell1, $Sell3);


                    if ($percentage <-$SellInput1_2) {
						$result=GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3);
						$StartDate=$result[0];
						$displays=$result[1];
						$technicals=$result[2];
						$type=$result[3];

      					echo"<tr>";
						echo"<td>Sell Signal found for $Symbol, using $Name strategy: Sell when: $Sell1_1($SellInput1_1)=[$Sell1] is at least $SellInput1_2% below $Sell1_3($SellInput1_3)=[$Sell3]
						<a href='stockMonitoringChart.php?displays=$displays&advSymbol=$Symbol&advStartDate=$StartDate&advType=$type&advEndDate=$EndDate&technicals=$technicals' target='_blank'>See Chart</a></td>";
						echo"</tr>";
                    }
                    break;
            }
			

            
        
        
        $indexer+=1;
    }
		
	}
	
	}
	echo "</table>";
}

    


function CalculatePercentage($Buy1, $Buy3) {

    //calculation
    $percentage = $Buy3 / $Buy1;

    $percentage = 1 - $percentage;
    $percentage = $percentage * 100;

    //return percentage
    return $percentage;
}

//Queries database to get the previous $number rows dates
function obtainDate($number,$con,$symbol,$startDate){

	$newStartDate="";
	//get the correct starting date- eg.SMA: number dates to average
	
		$result = mysqli_query($con,"SELECT * FROM (
	SELECT * FROM `shares` WHERE `Symbol`='$symbol' AND `Date`<'$startDate' Order by `Date` DESC Limit $number
	)sub
	Order by `Date` asc limit 1
	");
		
		
				while($row = mysqli_fetch_array($result)) {
					$newStartDate=$row['Date'];
						
				
	
		
}	
return $newStartDate;
}

function GetChartURLDetales($con,$Symbol,$StartDate,$Buy1_1,$BuyInput1_1,$Buy1_3,$BuyInput1_3){
	//returns the URL details needed for the charting page to draw the chart
						$displays=array();
						$technicals=array();
						$type="Close";
						if($Buy1_1=="SMA"||$Buy1_1=="EMA"||$Buy1_1=="WMA"||$Buy1_1=="SAR"){
							array_push($displays,$Buy1_1.'/'.$BuyInput1_1);	
							
						}
						if($Buy1_1=="BollingerLower"||$Buy1_1=="BollingerUpper"){
							array_push($displays,"Bollinger".'/'.$BuyInput1_1);	
							
						}
						if($Buy1_3=="SMA"||$Buy1_3=="EMA"||$Buy1_3=="WMA"||$Buy1_3=="SAR"){
							array_push($displays,$Buy1_3.'/'.$BuyInput1_3);	
							
						}
						if($Buy1_3=="BollingerLower"||$Buy1_3=="BollingerUpper"){
							array_push($displays,"Bollinger".'/'.$BuyInput1_3);	
							
						}
						
						if($Buy1_1=="Close"||$Buy1_1=="High"||$Buy1_1=="Low"||$Buy1_1=="Open"){
							$type=$Buy1_1;	
						}
						if($Buy1_3=="Close"||$Buy1_3=="High"||$Buy1_3=="Low"||$Buy1_3=="Open"){
							$type=$Buy1_3;	
						}
						
						if($Buy1_1=="RSI"||$Buy1_1=="MacD"||$Buy1_1=="ROC"||$Buy1_1=="OBV"||$Buy1_1=="WilliamR"||$Buy1_1=="MTM"||$Buy1_1=="FastSTC"||$Buy1_1=="PVT"){
							array_push($technicals,$Buy1_1.'/'.$BuyInput1_1);	
							
						}
						if($Buy1_1=="MacDSignal"){
							array_push($technicals,"MacD".'/'.$BuyInput1_1);
							
						}
						if($Buy1_1=="DMIplus"||$Buy1_1=="DMIminus"){
							array_push($technicals,"DMI".'/'.$BuyInput1_1);
							
						}
						
						if($Buy1_3=="RSI"||$Buy1_3=="MacD"||$Buy1_3=="ROC"||$Buy1_3=="OBV"||$Buy1_3=="WilliamR"||$Buy1_3=="MTM"||$Buy1_3=="FastSTC"||$Buy1_3=="PVT"){
							array_push($technicals,$Buy1_3.'/'.$BuyInput1_3);	
							
						}
						if($Buy1_3=="MacDSignal"){
							array_push($technicals,"MacD".'/'.$BuyInput1_3);
							
						}
						if($Buy1_3=="DMIplus"||$Buy1_3=="DMIminus"){
							array_push($technicals,"DMI".'/'.$BuyInput1_3);
							
						}
						
						
						
						
						$displays=implode(",",$displays);
						$technicals=implode(",",$technicals);
						
						//get new start date, current startdate minus 7 days.
						$StartDate=obtainDate(7,$con,$Symbol,$StartDate);
						$result=array($StartDate, $displays, $technicals, $type);
						return $result;	
	
}

?>
  